import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/components/common/not_found.dart';
import 'package:cbec_app/components/common/recommend.dart';
import 'package:cbec_app/components/order/order_item.dart';
import 'package:cbec_app/pages/my/controller/my_order_ctr.dart';
import 'package:cbec_app/utils/AppData.dart';
import 'package:cbec_app/utils/ThemeConfig.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

/// 我的订单
class MyOrder extends GetView<MyOrderController> {
  MyOrder({super.key});

  final MyOrderController myOrderCtr = Get.put((MyOrderController()));

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MyOrderController>(builder: (controller) {
      return Scaffold(
        appBar: AppBar(
          title: Text(
            '我的订单',
            style: TextStyle(fontWeight: FontWeight.w500, fontSize: 17, color: Tools.fromHex('#333333')),
          ),
          centerTitle: true,
          backgroundColor: Colors.white, // 设置透明
          leading: IconButton(
            icon: const Icon(
              Icons.arrow_back_ios,
              color: Colors.black,
            ),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        ),
        body: Stack(
          children: [
            // 主内容区域
            Column(
              children: [
                //搜索框
                _search(),
                //订单状态筛选
                _tabs(),
                Expanded(
                  child: controller.orderList.isNotEmpty
                      ? Tools.getPullRefresh(
                          enablePullDown: true,
                          enablePullUp: true,
                          refreshController: controller.baseRefreshCtr,
                          onRefresh: controller.onPageRefresh,
                          onLoading: controller.onPageLoading,
                          child: ListView.builder(
                              padding: EdgeInsets.zero,
                              itemCount: controller.orderList.length,
                              shrinkWrap: true,
                              itemBuilder: (context, index) {
                                final List item = controller.orderList;

                                return OrderItem(
                                  isHideComment: controller.initTabIndex == 0,
                                  orderSn: item[index]['id'],
                                  orderTime: item[index]['cd'],
                                  totalAmount: item[index]['priceOrder'],
                                  status: item[index]['status'],
                                  goodsList: item[index]['goodsInfo'],
                                  id: item[index]['id'],
                                  sn: item[index]['sn'],
                                  paySn: item[index]['paySn'],
                                  priceOrder: item[index]['priceOrder'],
                                  type: item[index]['type'],
                                  presaleData: item[index],
                                  isPresale: item[index]['type'].toString() == '5',
                                  hasBonded: item[index]['goodsInfo'].any((item) => item['commodityType'] == 1),
                                );
                              }))
                      : const NotFound(),
                ),
              ],
            ),
            // 底部浮动按钮
            Visibility(
              visible: controller.showOldOrder,
              child: Positioned(
                bottom: 16.0, // 设置距离底部的间距
                right: 16.0, // 设置距离右侧的间距
                left: 16.0,
                child: FloatingActionButton(
                  onPressed: () {
                    controller.goToBdHistoryOrders();
                  },
                  backgroundColor: Tools.fromHex("F83F2D"), // 设置按钮颜色
                  child: const Text(
                    '查看老系统历史订单',
                    style: TextStyle(color: Colors.white),
                  ), // 按钮图标
                ),
              ),
            ),
          ],
        ),
      );
    });
  }

  //上方搜索框
  Widget _search() {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(
          color: Tools.fromHex("#D7D7D7"),
          width: 1.0,
        ),
        borderRadius: BorderRadius.circular(50.0),
      ),
      height: 30.h,
      margin: EdgeInsets.only(top: 10.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(
            width: 5.w,
          ),
          Icon(
            Icons.search,
            color: Tools.fromHex("#999999"),
          ),
          SizedBox(
            width: 5.w,
          ),
          SizedBox(
            width: 200.w,
            child: TextField(
              controller: controller.searchTxt,
              onSubmitted: (value) {
                controller.search(value);
              },
              decoration: descript(
                "搜索我的订单",
                fontsize: 13,
              ),
            ),
          )
        ],
      ),
    );
  }

  /// 订单状态
  Widget _tabs() => Container(
        margin: const EdgeInsets.only(top: 10, bottom: 10),
        // height: 22,
        width: ScreenUtil().screenWidth,
        child: TabBar(
            dividerColor: Colors.transparent,
            indicator: const BoxDecoration(
              border: Border(
                bottom: BorderSide(width: 2, color: Colors.red),
              ),
            ),
            labelColor: Tools.fromHex("#F83F2D"),
            unselectedLabelColor: Tools.fromHex('#333333'),
            controller: myOrderCtr.statusTabController,
            tabAlignment: TabAlignment.fill,
            labelPadding: const EdgeInsets.symmetric(horizontal: 0),
            labelStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.w700, color: Tools.fromHex('#F83F2D')),
            unselectedLabelStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.w400, color: Tools.fromHex('#333333')),
            tabs: const [
              Tab(
                text: "全部",
              ),
              Tab(
                text: "待付款",
              ),
              Tab(
                text: "待发货",
              ),
              Tab(
                text: "待收货",
              ),
              Tab(
                text: "已取消",
              ),
              Tab(
                text: "已完成",
              ),
            ]),
      );
}
